package com.yonyou.pmclouds.stagecheck.process;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yonyou.pmclouds.basecom.bp.processor.SingleProcessor;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.projectlog.entity.OtherVo;
import com.yonyou.pmclouds.projectlog.entity.ProjectLogDetailVo;
import com.yonyou.pmclouds.projectlog.entity.ProjectLogJobVo;
import com.yonyou.pmclouds.projectlog.entity.StageCheckContentVO;
import com.yonyou.pmclouds.projectlog.rmiitf.ProjectLogMaintain;
import com.yonyou.pmclouds.reason.rmiitf.ProblemReasonQuery;
import com.yonyou.pmclouds.stagecheck.entity.StageCheckVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author chulf
 * @date 2020/4/1
 */
@Slf4j
@Component
public class StageCheckProjectLogPostProcess implements SingleProcessor<StageCheckVO> {

    private static ObjectMapper objectMapper = new ObjectMapper();

    @Autowired
    private ProjectLogMaintain projectLogMaintain;
    @Autowired
    private ProblemReasonQuery problemReasonQuery;

    @Override
    public void process(StageCheckVO vo, StageCheckVO orig) throws BusinessException {

        try {
            //最终放入队列的任务对象
            ProjectLogJobVo jobVo = new ProjectLogJobVo();

            jobVo.setPkTenant(vo.getPkTenant());
            jobVo.setPkProject(vo.getPkProject());
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            jobVo.setLogDate(calendar.getTime());
            jobVo.setEventType(ProjectLogJobVo.EVENT_TYPE_DETAIL);
            jobVo.setEventTime(vo.getCreationtime());
            jobVo.setUserId(vo.getCreator());

            // 生成项目日志子表
            ProjectLogDetailVo detailVo = generateDetailVO(vo);
            jobVo.setAttachment(detailVo);

            projectLogMaintain.upsert(jobVo);
        } catch (Exception e) {
            log.error("阶段验收单--智能日志处理出错，阶段验收pk：{}", vo.getPkStageCheck());
            log.error("异常信息", e);
        }

}

    /**
     * 生成项目日志详情VO
     *
     * @param vo
     * @return
     */
    private ProjectLogDetailVo generateDetailVO(StageCheckVO vo) throws BusinessException, JsonProcessingException {
        ProjectLogDetailVo logDetail = new ProjectLogDetailVo();
        logDetail.setDetailType(ProjectLogDetailVo.OTHER);
        logDetail.setPkContentSource(vo.getPkStageCheck());
        logDetail.setCreator(vo.getCreator());
        logDetail.setCreationtime(vo.getCreationtime());
        logDetail.setModifier(vo.getModifier());
        logDetail.setModifytime(vo.getModifytime());
        OtherVo contentVO = generateContentVO(vo);
        logDetail.setContentJson(objectMapper.writeValueAsString(contentVO));

        return logDetail;
    }

    /**
     * 生成界面显示的内容VO
     *
     * @param vo
     * @return
     */
    private OtherVo generateContentVO(StageCheckVO vo) throws BusinessException {
        OtherVo contentVO = new OtherVo();
        contentVO.setPkBill(vo.getPkStageCheck());
        contentVO.setBillType(BillTypeConst.STAGECHECK_BILLTYPE);
        /**
         *  待补充模板显示内容
         */
        contentVO.setSummary(MessageFormat.format(OtherVo.SUMMARY_TEMPLATE_STAGECHECK,
                StringUtils.isEmpty(vo.getScheduleItemName()) ? "" : vo.getScheduleItemName()+"任务", vo.getRate()+"%"));

        return contentVO;
    }
}
